package com.graduation.sports.db;

import com.blankj.utilcode.util.SPUtils;
import com.graduation.sports.bean.CommunityInfo;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.bean.SportMotionRecord;
import com.graduation.sports.bean.UserAccount;
import com.graduation.sports.listener.OnInsertListener;
import com.graduation.sports.util.DateUtils;

import java.util.List;

public class DataManager implements DBHelper {

    private RealmHelper realmHelper;
    private static final String IS_FIRST_INIT = "IS_FIRST_INIT";

    private DataManager() {

    }

    public DataManager(RealmHelper helper ) {
        realmHelper = helper;
    }

    public boolean isFirstInit() {
        boolean isFirstInit = SPUtils.getInstance().getBoolean(IS_FIRST_INIT, true);
        if (isFirstInit) {
            SPUtils.getInstance().put(IS_FIRST_INIT, false);
            return true;
        }
        return false;
    }

    public void initTextData(){
        for (int i = 0; i < 8; i ++ ){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setGroupName("小组" + i);
            groupInfo.setGroupImg("http://www.ico51.cn/images/logo.png");
            insertGroupInfo(groupInfo);
        }

        for (int j = 0; j < 20; j++) {
            CommunityInfo communityInfo = new CommunityInfo();
            communityInfo.setContent("我是发布的消息" + j);
            communityInfo.setTimeStamp(DateUtils.convertToString(System.currentTimeMillis()));
            communityInfo.setUserAccount("aaa");
            communityInfo.setUserName("张三" + j);
            insertCommunity(communityInfo);
        }
    }

    @Override
    public void insertSportRecord(SportMotionRecord record) {
        realmHelper.insertSportRecord(record);
    }

    @Override
    public void deleteSportRecord(SportMotionRecord record) {
        realmHelper.deleteSportRecord(record);
    }

    @Override
    public void deleteSportRecord() {
        realmHelper.deleteSportRecord();
    }

    @Override
    public List<SportMotionRecord> queryRecordList(int master) {
        return realmHelper.queryRecordList(master);
    }

    @Override
    public List<SportMotionRecord> queryRecordList(int master, String dateTag) {
        return realmHelper.queryRecordList(master, dateTag);
    }

    @Override
    public List<SportMotionRecord> queryRecordList() {
        return realmHelper.queryRecordList();
    }

    @Override
    public SportMotionRecord queryRecord(int master, long startTime, long endTime) {
        return realmHelper.queryRecord(master, startTime, endTime);
    }

    @Override
    public SportMotionRecord queryRecord(int master, String dateTag) {
        return realmHelper.queryRecord(master, dateTag);
    }

    @Override
    public void closeRealm() {
        realmHelper.closeRealm();
    }

    @Override
    public void insertAccount(UserAccount account) {
        realmHelper.insertAccount(account);
    }

    @Override
    public UserAccount queryAccount(String account) {
        return realmHelper.queryAccount(account);
    }

    @Override
    public boolean checkAccount(String account, String psd) {
        return realmHelper.checkAccount(account, psd);
    }

    @Override
    public boolean checkAccount(String account) {
        return realmHelper.checkAccount(account);
    }

    @Override
    public List<UserAccount> queryAccountList() {
        return realmHelper.queryAccountList();
    }

    @Override
    public List<GroupInfo> queryGroupInfoList() {
        return realmHelper.queryGroupInfoList();
    }

    @Override
    public void insertGroupInfo(GroupInfo groupInfo) {
        realmHelper.insertGroupInfo(groupInfo);
    }

    @Override
    public List<CommunityInfo> queryCommunityInfoList() {
        return realmHelper.queryCommunityInfoList();
    }

    @Override
    public void insertCommunity(CommunityInfo communityInfo) {
        realmHelper.insertCommunity(communityInfo);
    }
}
